import { type NextRequest, NextResponse } from "next/server"
import type { BackendPaginatedResponse, ApiResponse, Sample } from "@/types"

// 配置为静态路由以支持静态导出
export const dynamic = 'force-static'

export async function GET(request: NextRequest) {
  try {
    // 验证token
    const token = request.headers.get("authorization")?.replace("Bearer ", "")
    if (!token) {
      return NextResponse.json({ success: false, message: "未授权" }, { status: 401 })
    }

    const { searchParams } = new URL(request.url)

    // 这里应该调用真实的后端API
    // const response = await fetch(`http://localhost:8081/api/samples?${searchParams}`, {
    //   headers: { Authorization: `Bearer ${token}` }
    // })
    // const result = await response.json()
    // return NextResponse.json(result)

    // 临时模拟数据，匹配后端实际返回格式
    const mockData: BackendPaginatedResponse<Sample> = {
      dataList: [
        {
          id: "4",
          sampleNumber: "337924851408006",
          sampleName: "土壤样品F",
          sampleType: "土壤样品",
          testingUnit: "环境检测中心",
          mixedNumber: "MX001",
          samplingNumber: null,
          operator: "张三",
          operationTime: new Date("2025-01-15T10:30:00"),
        },
        {
          id: "5",
          sampleNumber: "337924851408007",
          sampleName: "土壤样品G",
          sampleType: "土壤样品",
          testingUnit: "环境检测中心",
          mixedNumber: "MX001",
          samplingNumber: null,
          operator: "张三",
          operationTime: new Date("2025-01-15T10:30:00"),
        },
        {
          id: "6",
          sampleNumber: "337924851408009",
          sampleName: "水质样品I",
          sampleType: "水质样品",
          testingUnit: "水质检测站",
          mixedNumber: "MX002",
          samplingNumber: null,
          operator: "李四",
          operationTime: new Date("2025-01-16T14:45:00"),
        },
      ],
      total: 2, // 组数（MX001和MX002两组）
      page: 1,
      pageSize: 10,
      totalSamples: 3,
    }

    const response: ApiResponse<BackendPaginatedResponse<Sample>> = {
      success: true,
      data: mockData,
      message: "", // 添加必填的message属性
    }

    return NextResponse.json(response)
  } catch (error) {
    return NextResponse.json({ success: false, message: "服务器错误" }, { status: 500 })
  }
}
